from typing import List
from sqlalchemy.orm import Session

from app.dao.model.compute import Compute


def delete_compute_by_service_id(db: Session, service_id: str) -> int:
    compute = db.query(Compute).\
        filter(Compute.service_id == service_id).\
        first()
    db.delete(compute)
    db.commit()


def get_compute_count(db: Session) -> int:
    return db.query(Compute).count()


def get_compute(db: Session, *, page=1, size=20) -> List[Compute]:
    return db.query(Compute).offset((page-1)*size).limit(size).all()


def create_compute(db: Session, *, compute: Compute = None, computes: List[Compute] = None):
    if computes is not None:
        db.add_all(computes)
    if compute is not None:
        db.add(compute)

    db.commit()


def modify_compute(db: Session, service_id: str, scale: int) -> int:
    compute = db.query(Compute).\
        filter(Compute.service_id == service_id).\
        first()
    compute.size = scale
    db.commit()
